package com.ldc.common.utils;

import java.text.SimpleDateFormat;
import java.util.Calendar;
import java.util.Date;
import java.util.TimeZone;

/**
 * 功能说明
 *
 * @auther liudc
 * @since 2023/6/11
 */
public class DateTimeUtils {

    public DateTimeUtils() {
        throw new IllegalStateException("非法创建");
    }

    public static final String DEFAULT_TIME_ZONE = "UTC";
    public static final String PATTERN_YYYY_MM_DD_HH_MM = "yyyy-MM-dd HH:mm";
    public static final String PATTERN_YYYY_MM_DD = "yyyy-MM-dd";
    public static final String PATTERN_YYYY_MM_DD_ss = "yyyy-MM-dd HH:mm:ss";


    public static String getDateTimeNow(String pattern) {
        return getDateTime(new Date(), pattern);
    }

    public static String getDateTimeNow() {
        return getDateTime(new Date());
    }

    public static String getDateTime(Date date) {
        return getDateTime(date, PATTERN_YYYY_MM_DD_HH_MM);
    }

    public static String getDateTime(Date date, String pattern) {
        SimpleDateFormat simpleDateFormat = new SimpleDateFormat(pattern);
        return simpleDateFormat.format(date);
    }

    /**
     * 获取日期
     *
     * @return
     */
    public static Date getDate(long time) {
        return new Date(time);
    }

    /**
     * 获取日期
     *
     * @param zone 时区
     * @return
     */
    public static Date getDate(TimeZone zone) {
        return getCalendar(zone).getTime();

    }

    /**
     * 获取日期
     *
     * @return
     */
    public static Date getDate() {
        return getDate(getTimZoneUTC());
    }

    /**
     * 获取当前时间
     *
     * @return
     */
    public static long getTime(TimeZone zone) {
        return getCalendar(zone).getTimeInMillis();
    }

    /**
     * 获取时间
     *
     * @return
     */
    public static long getTime() {
        return getTime(getTimZoneUTC());
    }

    /**
     * 获取日历
     *
     * @param zone 时区
     * @return
     */
    public static Calendar getCalendar(TimeZone zone) {
        return Calendar.getInstance(zone);
    }

    /**
     * 获取日历
     *
     * @return
     */
    public static Calendar getCalendar() {
        return getCalendar(getTimZoneUTC());
    }

    /**
     * 获取时区
     *
     * @return
     */
    public static TimeZone getTimZoneUTC() {
        return getTimeZone(DEFAULT_TIME_ZONE);
    }

    /**
     * 获取时区
     *
     * @param timeZoneId
     * @return
     */
    public static TimeZone getTimeZone(String timeZoneId) {
        return TimeZone.getTimeZone(timeZoneId);
    }

    /**
     * 日期相加
     *
     * @param date
     * @param day
     * @return
     */
    public static Date addDay(Date date, int day) {
        return addDay(date, day, getTimZoneUTC());
    }

    /**
     * 日期相加
     *
     * @param date
     * @param day
     * @param zone
     * @return
     */
    public static Date addDay(Date date, int day, TimeZone zone) {
        Calendar calendar = getCalendar(zone);
        calendar.setTime(date);
        calendar.add(Calendar.DAY_OF_MONTH, day);
        return calendar.getTime();
    }

    /**
     * 加小时
     *
     * @param time
     * @param hour
     * @return
     */
    public static Date addHour(long time, int hour) {
        return addHour(time, hour, getTimZoneUTC());
    }

    /**
     * 加小时
     *
     * @param time
     * @param hour
     * @param zone
     * @return
     */
    public static Date addHour(long time, int hour, TimeZone zone) {
        Calendar calendar = getCalendar(zone);
        calendar.setTimeInMillis(time);
        calendar.add(Calendar.HOUR, hour);
        return calendar.getTime();
    }
}
